<?php


namespace App\Http\Controllers\Api\Finance;


use App\Exports\PassengerExport;
use App\Exports\WithdrawExport;
use App\Http\Controllers\Controller;
use App\Models\CompanyManagement;
use App\Models\OrderFlow;
use App\Repositories\DriverFinancialRepository;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;

class FinancialFlowController extends Controller
{

    /**
     * 乘客流水明细列表
     * @param Request $request
     * @param DriverFinancialRepository $repository
     * @return FinancialFlowController|JsonResponse
     * @Author 姿势就是力量
     */
    public function getPageList(Request $request, DriverFinancialRepository $repository)
    {
        $companyId = (int)$request->get('company_id', '');
        $pageSize = (int)$request->get('page_size', 10);
        $list = $repository->getPageList($companyId, $pageSize);
        $list['count'] = $repository->totalMoney($companyId);
        return $this->sendJson($list);
    }
    /**
     * 流水明细列表
     * @param Request $request
     * @return FinancialFlowController|\Illuminate\Http\JsonResponse
     * @Author 姿势就是力量
     */
    public function driver(Request $request)
    {

        $companyId = (int)$request->get('company_id', '');   //公司id
        $real_name = (string)$request->get('real_name', '');//司机姓名
        $real_phone = (int)$request->get('phone', '');//司机手机号
        $u_phone = (int)$request->get('u_phone', '');//客户手机号
        $number= (string)$request->get('number', '');//车牌号
        $pay_sn = (string)$request->get('pay_sn', '');  //流水号
        $order_sn = (string)$request->get('order_sn', '');  //订单号
        $price = (string)$request->get('price', '');  //流水金额
        $reality_start = (string)$request->get('reality_start', '');  //实际起始地
        $reality_end = (string)$request->get('reality_end', '');  //实际目的地
        $service_name = (int)$request->get('service_name', '');  //业务类型
        $date =(string)$request->get('created_at','');      //时间选择
        $pageSize = (int)$request->get('page_size',20);
        $list = DB::table('order_flow')
            //关联订单表
            ->leftJoin('orders', 'order_flow.order_id', '=', 'orders.id')
            //关联订单路程表
            ->leftJoin('order_distance','orders.id','=','order_distance.order_id')
            //关联业务类型表
            ->leftJoin('service_type','orders.service_type_id','=','service_type.id')
            //关联申请司机表
            ->leftJoin('drivers','orders.driver_id','=','drivers.id')
            //关联车辆表
            ->leftJoin('car','orders.car_id','=','car.id')
            //关联用户表
            ->leftJoin('users as u','orders.user_id','=','u.id')
            ->select('order_flow.*', 'orders.company_id','orders.service_type_id','service_type.name','u.phone as u_phone','drivers.real_name','drivers.phone', 'order_distance.reality_start','order_distance.reality_end','car.number')
            ->when($real_name, function ($query) use ($real_name) {
                $query->where('drivers.real_name', 'like', '%' . $real_name . '%');
            }) //查询司机姓名
            ->when($real_phone, function ($query) use ($real_phone) {
                $query->where('drivers.phone', 'like', '%' . $real_phone . '%');
            })//查询司机手机号
            ->when($number, function ($query) use ($number) {
                $query->where('car.number', 'like', '%' . $number . '%');
            })//查询车辆号码
            ->when($pay_sn, function ($query) use ($pay_sn) {
                $query->where('order_flow.pay_sn', 'like', '%' . $pay_sn . '%');
            })//查询流水号
            ->when( $order_sn, function ($query) use ( $order_sn) {
                $query->where('order_flow.order_sn', 'like', '%' . $order_sn . '%');
            })//查询订单号
            ->when($reality_start, function ($query) use ($reality_start) {
                $query->where('order_distance.reality_start', 'like', '%' .$reality_start . '%');
            })//实际起始地
            ->when($reality_end, function ($query) use ($reality_end) {
                $query->where('order_distance.reality_end', 'like', '%' . $reality_end . '%');
            })//实际目的地
            ->when($price, function ($query) use ( $price) {
                $query->where('order_flow.price', 'like', '%' . $price . '%');
            })//查询流水金额
            ->when( $u_phone, function ($query) use (  $u_phone) {
                $query->where('u.phone', 'like', '%' . $u_phone . '%');
            })//查询客户手机号
            ->when($service_name, function ($query) use ($service_name) {
                $query->where('service_type.id', 'like', '%' . $service_name . '%');
            })//查询业务类型
            ->when($companyId, function ($query) use ($companyId) {
                $query->where('orders.company_id', 'like', '%' . $companyId . '%');
            })//查询公司
            ->when($date, function ($query) use ($date) {
                if($date){
                    $list = explode(',',$date);
                    $query->whereBetween('order_flow.created_at', [$list[0],$list[1]]);  //时间选择
                }
            }) //时间搜索
            ->whereIn('orders.company_id', session('companyIds'))
            ->orderBy('id','desc')
            ->paginate( $pageSize)
            ->toArray();


        return $this->sendJson($list);
    }


    /**
     * 导出乘客流水明细列表
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     * @Author 姿势就是力量
     */
    public function driverExcel(Request $request)
    {

        $user_id = $request->get('management_id','');  //查所属公司
        $company = (new CompanyManagement())->where('management_id',$user_id)->get()->toArray();  //查所属公司
        $companyIds =array_column($company, 'company_id');   //查所属公司
        $companyId = (int)$request->get('company_id', '');   //公司id
        $real_name = (string)$request->get('real_name', '');//司机姓名
        $real_phone = (int)$request->get('phone', '');//司机手机号
        $u_phone = (int)$request->get('u_phone', '');//客户手机号
        $number= (string)$request->get('number', '');//车牌号
        $pay_sn = (string)$request->get('pay_sn', 	'');  //流水号
        $order_sn = (string)$request->get('order_sn', 	'');  //订单号
        $price = (string)$request->get('price', '');  //流水金额
        $reality_start = (string)$request->get('reality_start', '');  //实际起始地
        $reality_end = (string)$request->get('reality_end', '');  //实际目的地
        $service_name = (int)$request->get('service_name', '');  //业务类型
        $date =(string)$request->get('created_at','');      //时间选择
        $list = DB::table('order_flow')
            //关联订单表
            ->leftJoin('orders', 'order_flow.order_id', '=', 'orders.id')
            //关联订单路程表
            ->leftJoin('order_distance','orders.id','=','order_distance.order_id')
            //关联业务类型表
            ->leftJoin('service_type','orders.service_type_id','=','service_type.id')
            //关联申请司机表
            ->leftJoin('drivers','orders.driver_id','=','drivers.id')
            //关联车辆表
            ->leftJoin('car','orders.car_id','=','car.id')
            //关联用户表
            ->leftJoin('users as u','orders.user_id','=','u.id')
            ->select('order_flow.*', 'orders.company_id','orders.service_type_id','service_type.name','u.phone as u_phone','drivers.real_name','drivers.phone', 'order_distance.reality_start','order_distance.reality_end','car.number')
            ->when($real_name, function ($query) use ($real_name) {
                $query->where('drivers.real_name', 'like', '%' . $real_name . '%');
            }) //查询司机姓名
            ->when($real_phone, function ($query) use ($real_phone) {
                $query->where('drivers.phone', 'like', '%' . $real_phone . '%');
            })//查询司机手机号
            ->when($number, function ($query) use ($number) {
                $query->where('car.number', 'like', '%' . $number . '%');
            })//查询车辆号码
            ->when($pay_sn, function ($query) use ($pay_sn) {
                $query->where('order_flow.pay_sn', 'like', '%' .$pay_sn. '%');
            })//查询流水号
            ->when( $order_sn, function ($query) use ( $order_sn) {
                $query->where('order_flow.order_sn', 'like', '%' . $order_sn . '%');
            })//查询订单号
            ->when($reality_start, function ($query) use ($reality_start) {
                $query->where('order_distance.reality_start', 'like', '%' .$reality_start . '%');
            })//实际起始地
            ->when($reality_end, function ($query) use ($reality_end) {
                $query->where('order_distance.reality_end', 'like', '%' . $reality_end . '%');
            })//实际目的地
            ->when($price, function ($query) use ( $price) {
                $query->where('order_flow.price', 'like', '%' . $price . '%');
            })//查询流水金额
            ->when( $u_phone, function ($query) use (  $u_phone) {
                $query->where('u.phone', 'like', '%' . $u_phone . '%');
            })//查询客户手机号
            ->when($service_name, function ($query) use ($service_name) {
                $query->where('service_type.id', 'like', '%' . $service_name . '%');
            })//查询业务类型
            ->when($companyId, function ($query) use ($companyId) {
                $query->where('orders.company_id', 'like', '%' . $companyId . '%');
            })//查询公司
            ->when($date, function ($query) use ($date) {
                if($date){
                    $list = explode(',',$date);
                    $query->whereBetween('order_flow.created_at', [$list[0],$list[1]]);  //时间选择
                }
            }) //时间搜索
            ->orderBy('id','desc')
            ->whereIn('orders.company_id',$companyIds)
            ->get();
        $row = [
            [
                "id" => 'ID',
                "created_at" => '流水时间',
                "pay_sn" => '流水号',
                "order_sn" => '订单号',
                "price" => '流水金额',
                "name" => '业务类型',
                "u_phone" => '客户手机号',
                "real_name" => '司机名称',
                "phone" => '司机手机号',
                "reality_start" => '实际起始地',
                "reality_end" => '实际目的地',
                "number" => '车牌号',
            ]
        ];

        return Excel::download(new  PassengerExport($row, $list), '乘客流水.xls');


    }

}
